package com.app.view.component.resource 
{
	import flash.display.DisplayObjectContainer;
	import flash.display.InteractiveObject;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.errors.IOError;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	
	import com.peachcastle.debug.Log;

	public class ResourceData extends EventDispatcher
	{
		
		public var uid:int;
		
		public var type:String;
		
		public var url:String;
		
		public var container:DisplayObjectContainer = null;
		
		public var loader:Loader = null;
		
		public var progress:Number;//加载进度;
		
		public var isShow:Boolean;//是否需要显示;
		
		public var iscomplete:Boolean = false;
		
		public var scale:int = 0;
		
		private var request:URLRequest = null;
		
		private var isstart:Boolean = false;
		
		
		public var dir:int = 0;
		
		
		public function ResourceData() 
		{
			loader = new Loader();
			loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onError);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
			request = new URLRequest();
		}
		
		public function start():void
		{
			if (url == "") return;
			if (isstart) return;
			isstart = true;
			isShow = true;
			request.url = url;
			loader.load(request);
		}
		
		
		public function getState():void
		{
			var sc:Number = 0;
			
			var move:int = 0;

			if (dir == 0)
			{
				if (loader.width < scale || loader.height < scale)
				{
					if (loader.width > loader.height)
					{
						sc = scale / loader.height;
						move = 1;
					}else
					{
						sc = scale / loader.width;
						move = 2;
					}
					
					loader.scaleX = loader.scaleY = sc;
					if (move == 1)
					{
						loader.x = -(loader.width-scale)>>1
					}else if (move == 2)
					{
						loader.y = -(loader.height-scale)>>1
					}
				}
			}
			if (dir == 1)
			{
				if (loader.width > scale || loader.height > scale)
				{
					if (loader.width > loader.height)
					{
						sc = scale / loader.height;
						move = 1;
					}else
					{
						sc = scale / loader.width;
						move = 2;
					}
					loader.scaleX = loader.scaleY = sc;
				}
				loader.x = -loader.width >> 1;
				loader.y = -loader.height >> 1;
				
				loader.x = 0;
				loader.y = 0;
			}
		}
		
		
		private function onProgress(event:ProgressEvent):void
		{
		}
		private function onComplete(event:Event):void
		{
			loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, onProgress);
			loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onComplete);
			iscomplete = true;
			dispatchEvent(event);
			
			
			getState();
			
		}
		
		
		
		private function onError(event:IOErrorEvent):void
		{
			
		}
	}
}